package operationplan

import (
	// "net/http"
	"encoding/json"
	"strconv"

	"time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"

	// "github.com/jinzhu/gorm"

	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/until"
)

type (
	operationplan struct {
		ID           bson.ObjectId `json:"_id" bson:"_id"`
		BatchNumber  string        `json:"batchnumber" bson:"batchnumber"`   //批次号
		StyleNumber  string        `json:"stylenumber" bson:"stylenumber"`   //款数
		CategoryId   string        `json:"categoryid" bson:"categoryid"`     //品类
		ProductKind  string        `json:"productkind" bson:"productkind"`   //品类
		TargetNumber string        `json:"targetnumber" bson:"targetnumber"` //目标数量
		TargetCost   string        `json:"targetcost" bson:"targetcost"`     //目标成本
		Value        string        `json:"value" bson:"value"`               //货值
		TargetPrice  string        `json:"targetprice" bson:"targetprice"`   //目标售价
		PremiumRate  string        `json:"premiumrate" bson:"premiumrate"`   //加价倍率
		Brand        string        `json:"brand" bson:"brand"`               //品牌
		BrandId      string        `json:"brandid" bson:"brandid"`           //品牌
		CreateTime   int64         `json:"createtime" bson:"createtime"`     //品牌
	}
	insertoperationplan struct {
		BatchNumber  string `json:"batchnumber" bson:"batchnumber"`   //批次号
		StyleNumber  string `json:"stylenumber" bson:"stylenumber"`   //款数
		CategoryId   string `json:"categoryid" bson:"categoryid"`     //品类
		ProductKind  string `json:"productkind" bson:"productkind"`   //品类
		TargetNumber string `json:"targetnumber" bson:"targetnumber"` //目标数量
		TargetCost   string `json:"targetcost" bson:"targetcost"`     //目标成本
		Value        string `json:"value" bson:"value"`               //货值
		TargetPrice  string `json:"targetprice" bson:"targetprice"`   //目标售价
		PremiumRate  string `json:"premiumrate" bson:"premiumrate"`   //加价倍率
		Brand        string `json:"brand" bson:"brand"`               //品牌
		BrandId      string `json:"brandid" bson:"brandid"`           //品牌
		CreateTime   int64  `json:"createtime" bson:"createtime"`     //品牌
	}
)

type OXYOperationPlanData struct{}

func NewOXYOperationPlanData() *OXYOperationPlanData {
	operationPlanData := &OXYOperationPlanData{}
	return operationPlanData
}

func (opd *OXYOperationPlanData) GetOperationPlanListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	sort := c.QueryParam("sort")
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "operation_plan"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	var query *mgo.Query
	count := 0
	if sort != "" {
		query, count, err = model.DB().QuerySort(M, currentpage, perpage, sort)
	} else {
		query, count, err = model.DB().Query(M, currentpage, perpage)
	}
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []operationplan{}
	query.All(&ps)

	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  string(str),
		"count": count,
	})

	return nil
}

func (opd *OXYOperationPlanData) GetOperationPlanInfoHandler(c echo.Context) error {
	log.Info("GetOperationPlanInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "operation_plan"
	query, err := model.DB().QueryOne(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p := operationplan{}
	query.One(&p)
	str, err := json.Marshal(p)
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
		"data": string(str),
	})

	return nil
}

func (opd *OXYOperationPlanData) AddOperationPlanHandler(c echo.Context) error {
	o := &insertoperationplan{}
	if err := c.Bind(o); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyCms"
	model.DB().Document = "operation_plan"
	timeTemplate1 := "2006-01-02 15:04:05" //常规类型
	t1 := o.BatchNumber[0:4] + "-" + o.BatchNumber[4:6] + "-" + o.BatchNumber[6:8] + " 15:04:05"
	stamp, _ := time.ParseInLocation(timeTemplate1, t1, time.Local)
	o.CreateTime = stamp.Unix() * 1000
	err := model.DB().Insert(o)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (opd *OXYOperationPlanData) UpdateOperationPlanHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateOperationPlanHandler", id)

	o := &insertoperationplan{}
	if err := c.Bind(o); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyCms"
	model.DB().Document = "operation_plan"
	timeTemplate1 := "2006-01-02 15:04:05" //常规类型
	t1 := o.BatchNumber[0:4] + "-" + o.BatchNumber[4:6] + "-" + o.BatchNumber[6:8] + " 15:04:05"
	stamp, _ := time.ParseInLocation(timeTemplate1, t1, time.Local)
	o.CreateTime = stamp.Unix() * 1000
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, o)
	if err != nil {
		log.Error("更新出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (opd *OXYOperationPlanData) DelOperationPlanHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelOperationPlanHandler", id)
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "operation_plan"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
